package test;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.util.logging.Logger;

import com.jme3.app.Application;
import com.jme3.system.AppSettings;

public class BaseAppSettings {
	private static final Logger logger = Logger.getLogger(Application.class
			.getName());

	private static final String filename = "config.cfg";

	public static AppSettings loadBaseSettings() {
		AppSettings settings = new AppSettings(true);

		File f = new File(filename);
		if (!f.exists()) {
			logger.warning("Did not found config.cfg");
			saveBaseSettings(settings);
			return settings;
		}

		try {
			BufferedReader input = new BufferedReader(new FileReader(f));
			try {
				String line;
				if ((line = input.readLine()) != null)
					settings.setWidth(new Integer(line.split("=")[1])
							.intValue());
				if ((line = input.readLine()) != null)
					settings.setHeight(new Integer(line.split("=")[1])
							.intValue());
				if ((line = input.readLine()) != null)
					settings.setBitsPerPixel(new Integer(line.split("=")[1])
							.intValue());
				if ((line = input.readLine()) != null)
					settings.setFrequency(new Integer(line.split("=")[1])
							.intValue());
				if ((line = input.readLine()) != null)
					settings.setFullscreen(Boolean.valueOf(line.split("=")[1])
							.booleanValue());
				if ((line = input.readLine()) != null)
					settings.setTitle(line.split("=")[1]);
				if ((line = input.readLine()) != null)
					settings.setRenderer(line.split("=")[1]);
				if ((line = input.readLine()) != null)
					settings.setAudioRenderer(line.split("=")[1]);
			} finally {
				input.close();
			}
		} catch (IOException e) {
			logger.warning("Tryed loading config.cfg, but got an error");
			e.printStackTrace();
		}

		return settings;
	}

	public static void saveBaseSettings(AppSettings settings) {
		File f = new File(filename);
		try {
			BufferedWriter output = new BufferedWriter(new FileWriter(f));
			try {
				output.write("Width="
						+ Integer.valueOf(settings.getWidth()).toString()
						+ "\n");
				output.write("Height="
						+ Integer.valueOf(settings.getHeight()).toString()
						+ "\n");
				output.write("BitsPerPixel="
						+ Integer.valueOf(settings.getBitsPerPixel())
								.toString() + "\n");
				output.write("Frequency="
						+ Integer.valueOf(settings.getFrequency()).toString()
						+ "\n");
				output.write("Fullscreen="
						+ Boolean.valueOf(settings.isFullscreen()).toString()
						+ "\n");
				output.write("Title=" + settings.isFullscreen() + "\n");
				output.write("Renderer=" + settings.getRenderer() + "\n");
				output.write("AudioRenderer=" + settings.getAudioRenderer()
						+ "\n");
			} finally {
				output.close();
			}
		} catch (IOException e) {
			logger.warning("Tryed save config.cfg, but got an error");
			e.printStackTrace();
		}
	}
}
